package android.tusnotas.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Log;

public class MD5Generator {

	
	private static String algorithm = "MD5";
	
	public static String generateMD5(String p) {

		MessageDigest m;
		String md5 = null;

		try {
			m = MessageDigest.getInstance(algorithm);
			m.update(p.getBytes(), 0, p.length());
			md5 = new BigInteger(1, m.digest()).toString(16);

		} catch (NoSuchAlgorithmException e) {
			Log.w(MD5Generator.class.getName(), "Error hashing the HTML. "+algorithm+" not supported");
			e.printStackTrace();
		}

		return md5;

	}

	public static String generateMD5(char[] a) {

		String p = charArrayToString(a);
		MessageDigest m;
		String md5 = null;

		try {

			m = MessageDigest.getInstance(algorithm);
			m.update(p.getBytes(), 0, p.length());
			md5 = new BigInteger(1, m.digest()).toString(16);

		} catch (NoSuchAlgorithmException e) {
			Log.w(MD5Generator.class.getName(), "Error hashing the HTML. "+algorithm+" not supported");
			e.printStackTrace();
		}

		return md5;

	}

	private static String charArrayToString(char[] a) {

		String r = "";
		for (char b : a)
			r += b;
		return r;
	}
}
